/*
	This script file is part of the Sonic 3 A.I.R. script software distribution.
	Copyright (C) 2017-2024 by Eukaryot

	Published under the GNU GPLv3 open source software license, see license.txt
	or https://www.gnu.org/licenses/gpl-3.0.en.html
*/



//-------------------------------------------------------//
// Platform regularly rocketing up, then crashing down   //
//-------------------------------------------------------//

//# address-hook(0x0430ea) end(0x043122)
function void fn0430ea()
{
	objA0.mapping_offset = 0x04324a
	objA0.sprite_attributes = (sprite_attribute.PALETTE.LINE2 | 0x03a1)
	objA0.render_flags |= render_flag.WORLD
	objA0.box_size.x = 0x40
	objA0.box_size.y = 0x20
	objA0.sprite_priority = 0x0280
	u16[A0 + 0x46] = objA0.position.y.u16
	u16[A0 + 0x38] = u16(objA0.subtype2c) * 8

	objA0.update_address = 0x043128
	fn043128()
}


//# address-hook(0x043128) end(0x043194)
function void fn043128()
{
	if (u8[A0 + 0x32] == 0)
	{
		D0.u16 = objA0.velocity.y
		objA0.velocity.y += 0x40
		D0.s32 = s32(D0.s16) << 8
		objA0.countdown_callback += D0
		D2.u16 = u16[A0 + 0x34]
		if (D2.u16 >= u16[A0 + 0x38])
		{
			objA0.velocity.y = 0
			u16[A0 + 0x34] = u16[A0 + 0x38]
			u8[A0 + 0x32] = 0x01
			u16[A0 + 0x3a] = 0x1e

			objA0.animation.frame = 0
			if (objA0.render_flags & render_flag.VISIBLE)
			{
				playSound(0x5f)
			}
		}
	}
	else
	{
		if (u16[A0 + 0x3a] == 0)
		{
			fn0431aa()
			return
		}

		--u16[A0 + 0x3a]
		objA0.animation.sprite = u8[0x043196 + objA0.animation.frame]
		if (objA0.animation.sprite != 0)
		{
			++objA0.animation.frame
		}
	}

	fn0431e0()
}


//# address-hook(0x0431aa) end(0x0432da)
function void fn0431aa()
{
	D2.u16 = u16[A0 + 0x34]
	if (D2.u16 != 0)
	{
		--D2.u16
		u16[A0 + 0x34] = D2.u16
		objA0.animation.sprite += 8
		objA0.animation.sprite &= 0x08

		D0.u8 = level.framecounter.low & 0x1f
		if (D0.u8 == 0)
		{
			playSound(0x48)
		}
	}
	else
	{
		u8[A0 + 0x32] = 0
		objA0.animation.sprite = 0
	}

	fn0431e0()
}


//# address-hook(0x0431e0) end(0x043244)
function void fn0431e0()
{
	D0.u16 = u16[A0 + 0x46] + u16[A0 + 0x34]
	objA0.position.y.u16 = D0.u16

	D1 = objA0.box_size.x + 11
	D2 = objA0.box_size.y - (objA0.animation.sprite & 0x07)
	D3.u16 = D2.u16 + 1
	D4.u16 = objA0.position.x.u16
	fn01dc56()

	D6 = (D6 >> 16) & 0x0c
	if (D6.u8 & 0x04)
	{
		A1 = 0xffffb000
		DamageCharacter()
	}

	if (D6.u8 & 0x08)
	{
		A1 = 0xffffb04a
		DamageCharacter()
	}

	DrawOrUnloadObject()
}
